// https://leetcode.cn/problems/candy/description/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    int candy(vector<int>& ratings) {
        int n = ratings.size(), minIndex = 0;
        vector<int> cd(n, 1);

        for (int i = 1; i < n; i++) {
            if (ratings[i] > ratings[i - 1]) {
                cd[i] = cd[i - 1] + 1;
            }
        }
        for (int i = n - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                cd[i] = max(cd[i], cd[i + 1] + 1);
            }
        }
        
        int ans = 0;
        for (int e : cd) {
            ans += e;
        }
        return ans;
    }
};